<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Article extends MY_Controller {

	public function __construct() {
		parent::__construct();
		$this->load->model('article_model', 'article');
	}
	public function index() {
		$this->load->library('pagination');
		$perPage = 3;

		$config['base_url'] = site_url('admin/article/index');
		$config['total_rows'] = $this->db->count_all_results('article');
		$config['per_page'] = $perPage;
		$config['uri_segment'] = 4;
		$config['first_link'] = '第一页';
		$config['prev_link'] = '上一页';
		$config['next_link'] = '下一页';
		$config['last_link'] = '最后一页';

		$this->pagination->initialize($config);
		$this->_data['links'] = $this->pagination->create_links();

		$offset = $this->uri->segment(4);
		$this->db->limit($perPage, $offset);

		$this->_data['articles'] = $this->article->get_articles();
		$this->load->view('admin/article_index.html', $this->_data);
	}
	/**
	 * 发表文章
	 * @return [type] [description]
	 */
	public function posts() {
		$aid = $this->uri->segment(4);
		if (FALSE == $aid) {
			$this->_write();
		} else {
			$this->_edit($aid);
		}
		
	}


	public function del() {
		$aid = $this->uri->segment(4);
		$this->article->delete($aid);
		redirect('admin/article');
	}

	private function _write() {
		$this->load->library('form_validation');
		$status = $this->form_validation->run('article');


		$this->load->model('category_model', 'cate');
		$cates = $this->cate->get_category();

		if (false == $status) {
			$this->_data['cates'] = $cates;
			$this->load->view('admin/posts.html', $this->_data);
		} else {
			//文件上传------------------------
			//配置
			$config['upload_path'] = './uploads/';
			$config['allowed_types'] = 'gif|jpg|png|jpeg';
			$config['max_size'] = '10000';
			$config['file_name'] = time() . mt_rand(1000,9999);

			//载入上传类
			$this->load->library('upload', $config);
			//执行上传
			$status = $this->upload->do_upload('thumb');
		
			$wrong = $this->upload->display_errors();

			if($wrong){
				error($wrong);
			}
			//返回信息
			$info = $this->upload->data();
			
						//缩略图-----------------
			//配置
			$arr['source_image'] = $info['full_path'];
			$arr['create_thumb'] = FALSE;
			$arr['maintain_ratio'] = TRUE;
			$arr['width'] = 200;
			$arr['height'] = 200;	

			//载入缩略图类
			$this->load->library('image_lib', $arr);
			//执行动作
			$status = $this->image_lib->resize();

			if(!$status){
				error('缩略图动作失败');
			}

			$data = array(
				'title'	=> $this->input->post('title'),
				'type'	=> $this->input->post('type'),
				'cid'	=> $this->input->post('cid'),
				'thumb'	=> $info['file_name'],
				'info'	=> $this->input->post('info'),
				'content'=> $this->input->post('content'),
				'time'	=> time()
				);
			$this->article->insert($data);
			redirect('admin/article');

		}
	}

	private function _edit($aid) {

		$this->load->library('form_validation');
		$status = $this->form_validation->run('article');

		if (false == $status) {
			$this->load->model('category_model', 'cate');
			$cates = $this->cate->get_category();

			$this->_data['cates'] = $cates;
			$this->_data['article'] = $this->article->get_article($aid);
			// dump($this->_data['article']);
			$this->load->view('admin/posts.html', $this->_data);
		} else {
			//文件上传------------------------
			//配置
			$config['upload_path'] = './uploads/';
			$config['allowed_types'] = 'gif|jpg|png|jpeg';
			$config['max_size'] = '10000';
			$config['file_name'] = time() . mt_rand(1000,9999);

			//载入上传类
			$this->load->library('upload', $config);
			//执行上传
			$status = $this->upload->do_upload('thumb');
		
			$wrong = $this->upload->display_errors();

			if($wrong){
				error($wrong);
			}
			//返回信息
			$info = $this->upload->data();
			
			//缩略图-----------------
			//配置
			$arr['source_image'] = $info['full_path'];
			$arr['create_thumb'] = FALSE;
			$arr['maintain_ratio'] = TRUE;
			$arr['width'] = 200;
			$arr['height'] = 200;	

			//载入缩略图类
			$this->load->library('image_lib', $arr);
			//执行动作
			$status = $this->image_lib->resize();

			if(!$status){
				error('缩略图动作失败');
			}

			$data = array(
				'title'	=> $this->input->post('title'),
				'type'	=> $this->input->post('type'),
				'cid'	=> $this->input->post('cid'),
				'thumb'	=> $info['file_name'],
				'info'	=> $this->input->post('info'),
				'content'=> $this->input->post('content'),
				'time'	=> time()
				);
			$this->article->edit($aid, $data);
			redirect('admin/article');
		}
	}	

}

/* End of file article.php */
/* Location: ./application/controllers/admin/article.php */